package com.atguigu.gmall.web.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.feign.seckill.SeckillFeignClient;
import com.atguigu.gmall.feign.user.UserInfoFeignClient;
import com.atguigu.gmall.order.entity.OrderInfo;
import com.atguigu.gmall.seckill.entity.SeckillGoods;
import com.atguigu.gmall.user.entity.UserAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class SeckillController {

    @Autowired
    private SeckillFeignClient seckillFeignClient ;

    @Autowired
    private UserInfoFeignClient userInfoFeignClient ;

    @GetMapping(value = "/seckill.html")
    public String index(Model model){
        Result<List<SeckillGoods>> seckillGoods = seckillFeignClient.findAllSeckillGoods();
        List<SeckillGoods> goodsList = seckillGoods.getData();
        model.addAttribute("list" , goodsList) ;
        return "seckill/index" ;
    }

    @GetMapping(value = "/seckill/{skuId}.html")
    public String item(@PathVariable(value = "skuId") Long skuId , Model model) {
        Result<SeckillGoods> result = seckillFeignClient.findBySkuId(skuId);
        SeckillGoods seckillGoods = result.getData();
        model.addAttribute("item" , seckillGoods) ;
        return "seckill/item" ;
    }

    @GetMapping(value = "/seckill/queue.html")
    public String queue(@RequestParam(value = "skuId") Long skuId , @RequestParam(value = "skuIdStr")String skuIdStr , Model model) {
        model.addAttribute("skuId" ,  skuId) ;
        model.addAttribute("skuIdStr" , skuIdStr) ;
        return "seckill/queue" ;
    }

    @GetMapping(value = "/seckill/trade.html")
    public String trade(@RequestParam(value = "skuId") Long skuId , Model model) {

        // 查询收货人的地址列表
        Result<List<UserAddress>> result = userInfoFeignClient.findByUserId();
        List<UserAddress> userAddressList = result.getData();

        // 查询订单明细数据
        Result<OrderInfo> orderInfoResult = seckillFeignClient.findOrderInfoBySkuId(skuId);
        OrderInfo orderInfo = orderInfoResult.getData();

        // 将数据存储到Model数据模型中，跳转页面
        model.addAttribute("userAddressList" , userAddressList) ;
        model.addAttribute("detailArrayList" , orderInfo.getOrderDetailList()) ;
        model.addAttribute("totalNum" , 1) ;
        model.addAttribute("totalAmount" , orderInfo.getTotalAmount()) ;

        return "seckill/trade" ;

    }

}
